// 转换附加的数据为数组格式
export function transformAttachToArray(attachData) {
    const result = [];
    if (attachData) {
        // 把 Map类型数据还原成 Array
        const addActionArr = attachData.add?.length
            ? attachData.add.map(item => ({ type: 'add', data: { ...item, append: true } }))
            : [];
        // 解构删除节点
        const deleteActionArr = attachData.delete?.length
            ? attachData.delete.map(item => ({
                type: 'delete',
                data: { ...item, deleted: true }
            }))
            : [];
        // 递归把树数据扁平化
        let queue = [...addActionArr, ...deleteActionArr];
        do {
            const current = queue.shift();
            if (current.data.children) {
                current.data.children = current.data.children.map(item => {
                    const newItem = { ...item, parentOrganizationPath: current.data.organizationPath, append: true };
                    return {
                        type: 'add',
                        data: newItem
                    };
                });
                queue = [...current.data.children, ...queue];
                delete current.data.children;
            }
            result.push(current);
        } while (queue.length > 0);
    }
    return result;
}

/**
 * 组织架构图中, 收缩节点时候, 判断节点是否在可视范围外面
 * @param el
 * @param offset
 * @returns {boolean|boolean}
 */
export function isElementInViewport(el, offset = 0) {
    const box = el.getBoundingClientRect();
    const top = box.top >= 0;
    const left = box.left - offset >= 0;
    const bottom = box.bottom <= (window.innerHeight || document.documentElement.clientHeight);
    const right = box.right + offset <= (window.innerWidth || document.documentElement.clientWidth);
    return top && left && bottom && right;
}
